import java.sql.*;
import java.util.*;

public class Main {
 public static void main(String[] args) {
     //m1();
     //m3();
     //m4();
     //m5();
 }

    private static void m1() {
                Connection connection = null;
                PreparedStatement preparedStatement = null;
                ResultSet resultSet = null;

                try {
                    Class.forName("com.mysql.cj.jdbc.Driver");

                    String url = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC";
                    connection = DriverManager.getConnection(url, "root", "121628");

                    String sql = "select * from user";

                    preparedStatement = connection.prepareStatement(sql);

                    resultSet = preparedStatement.executeQuery();


                    while (resultSet.next()) {
                        int id = resultSet.getInt(1);
                        String name = resultSet.getString(2);
                        String pass = resultSet.getString(3);
                        System.out.println(id + " " + name + " " + pass);
                    }

                    resultSet.close();
                    preparedStatement.close();
                    connection.close();


                } catch (ClassNotFoundException e) {
                    throw new RuntimeException(e);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                } finally {

                    try {
                        if (resultSet != null) {
                            resultSet.close();
                        }
                        if (preparedStatement != null) {
                            preparedStatement.close();
                        }
                        if (connection != null) {
                            connection.close();
                        }

                    } catch (SQLException e) {
                        throw new RuntimeException(e);
                    }

                }


    }
    private static void m3(){
        //键盘录入
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");

        String name = sc.nextLine();


                        Connection connection = null;
                PreparedStatement preparedStatement = null;
                ResultSet resultSet = null;

                try {
                    Class.forName("com.mysql.cj.jdbc.Driver");
                    String url = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC";
                    connection = DriverManager.getConnection(url, "root", "121628");
                    String sql = "insert into sys_user(name,pass) values(?,?)";
                    preparedStatement = connection.prepareStatement(sql);
                    //设置参数
                    preparedStatement.setString(1,"王五");
                    preparedStatement.setString(2,"123456");
                    int result = preparedStatement.executeUpdate();//执行增加

                    if(result > 0){
                        System.out.println("插入成功");
                    } else {
                        System.out.println("插入失败");
                    }

                }catch (ClassNotFoundException e) {
                    throw new RuntimeException(e);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }finally {
                    try {
                        if(preparedStatement != null) {
                            preparedStatement.close();
                        }
                        if(connection != null) {
                            connection.close();
                        }

                    } catch (SQLException e) {
                        throw new RuntimeException(e);
                    }

               }


    }

    private static void m4(){
        //键盘录入
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");

        String name = sc.nextLine();


        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC";
            connection = DriverManager.getConnection(url, "root", "121628");
            String sql = "update user set name = ?,pass = ? where id = ?";
            preparedStatement = connection.prepareStatement(sql);
            //设置参数
            preparedStatement.setString(1,"小明");
            preparedStatement.setString(2,"888");
            preparedStatement.setInt(3,7);
            int result = preparedStatement.executeUpdate();//执行增加

            if(result > 0){
                System.out.println("修改成功");
            } else {
                System.out.println("修改失败");
            }

        }catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                if(preparedStatement != null) {
                    preparedStatement.close();
                }
                if(connection != null) {
                    connection.close();
                }

            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }


    }

    private static void m5(){
        //键盘录入
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");

        String name = sc.nextLine();


        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC";
            connection = DriverManager.getConnection(url, "root", "121628");
            String sql = "delete from user where id = ?";
            preparedStatement = connection.prepareStatement(sql);
            //设置参数
            preparedStatement.setObject(1,7);
            int result = preparedStatement.executeUpdate();//执行增加

            if(result > 0){
                System.out.println("删除成功");
            } else {
                System.out.println("删除失败");
            }

        }catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                if(preparedStatement != null) {
                    preparedStatement.close();
                }
                if(connection != null) {
                    connection.close();
                }

            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }


    }
}

       //新增
